Ext.namespace("TR.admin.index");

TR.admin.index.topic = function(panelId, title, iconClz){

	var optBar = genOptBarByFn(create, del, update, toggle);

	var channelCombo = genChannelCombo();
	var statusCombo = genQueryStatusCombo();
	var queryField = genQueryField();
	var queryBar = ["->", "栏目:", channelCombo, "状态:", statusCombo, "关键词:", queryField, {
		text : "查询",
		iconCls : "query",
		handler : function(){
			topicStore.load();
		}
	}];
	var topicStore = new Ext.data.JsonStore({
		url : "admin/topic/findTopicByCon",
		fields : [
			"id", "title", "keyword", "status", "authorName", "channelName",
			"publishDate", "createDate"
		],
		totalProperty:"total",
		root : "rows",
		autoLoad : true,
		listeners : {
			beforeload : function(s){
				s.removeAll();
				Ext.apply(s.baseParams, {
					channelId : channelCombo.getValue(),
					status : statusCombo.getValue(),
					con : queryField.getValue()
				});
			}
		}
	});
	var topicSm = new Ext.grid.RowSelectionModel({
		singleSelect : true
	});
	var topicCm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
		header : "标题",
		dataIndex : "title",
		width : 300
	}, {
		header : "栏目",
		dataIndex : "channelName"
	}, {
		header : "关键词",
		dataIndex : "keyword",
		width : 200
	}, {
		header : "状态",
		dataIndex : "status",
		renderer : renderStatus
	}, {
		header : "作者",
		dataIndex : "authorName"
	}, {
		header : "创建时间",
		dataIndex : "createDate",
		renderer : renderDate
	}]);
	var topicGrid = new Ext.grid.GridPanel({
		store : topicStore,
		cm : topicCm,
		sm : topicSm,
		tbar : genToolBar(optBar, queryBar, panelId),
		bbar : genPageBar(topicStore)
	});
	return new Ext.Panel({
		id : panelId,
		title : title,
		iconCls : iconClz,
		closable : true,
		layout : "fit",
		items : [topicGrid]
	});

	function create(){
		window.open(ctx + "/admin/topic/add", "_blank");
	}
	function del(){
		var rec = topicSm.getSelected();
		if(rec==null){
			showFailMsg("请选择新闻");
			return;
		}
		confirmDelete(function(){
			ajaxRequest({
				url : "admin/topic/delete/" + rec.get("id"),
				callback : function(){
					topicStore.load();
				}
			});
		});
	}
	function update(){
		var rec = topicSm.getSelected();
		if(rec==null){
			showFailMsg("请选择新闻");
			return;
		}
		window.open(ctx + "/admin/topic/update/" + rec.get("id"), "_blank");
	}
	function toggle(){
		var rec = topicSm.getSelected();
		if(rec==null){
			showFailMsg("请选择新闻");
			return;
		}
		ajaxRequest({
			url : "admin/topic/toggleStatus/" + rec.get("id"),
			callback : function(){
				topicStore.load();
			}
		});
	}
}
registerModule("TR.admin.index.topic", TR.admin.index.topic);